'''
Created on Apr 21, 2011

@author: david
'''

from UI import *
from model.model import Log

from google.appengine.ext import webapp
from google.appengine.ext import db

HEAD = 1
TAIL = 0

class NewLog(webapp.RequestHandler):
    def get(self):
        "Output form"
        self.response.out.write(NEW_LOG_PG % self.list())
        
    def list(self):
        "retrieve and list emails from db"
        
        logs = db.GqlQuery("Select * from Log")
        
        html = "<table><tr><th>Log</th><th>H/T</th><th>Last</th></tr>"
        t = "<tr><td>%s</td><td>%s</td><td>%s</td></tr>"
        for log in logs:          
            d = "Head"
            if log.dir == TAIL:
                d = "Tail"
            html = "%s%s" % (html,t % (log.url, d,log.last))
        return "%s</table>" %  html
    

class UpdateLog(webapp.RequestHandler):
    def post(self):
        "add email to datastorage"
        # check for duplicate
        u = self.request.get('url')
        d = int(self.request.get('r1'))
        
        results = db.GqlQuery("select * from Log where url = '%s'" % u).fetch(1)
        
        if not len(results):
            ins = Log(url=u, dir=d, last='New Feed',status=1)
        else:
            ins = results
            
        ins.put()
        self.redirect("/logs/new")


class ListLogs(webapp.RequestHandler):
    pass
